// components/answer/index.js
var util = require('../../utils/util.js');
var t = function (t) {
        return t && t.__esModule ? t : {
            default: t
        };
    },
    app = getApp();

Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        imgbg: app.resPath + 'img/as_bg.jpg', //背景图片
        answer: [],
        list: [],
        //        logid: 0,
        index: 0,
        total: 0,
        select_index: -1,
        requestSuccess: !1,
        postSuccess: !1,
        info: {},
        timer: '', //定时器名字
        totaltimer: '',
        countNumNomal: 10, // 每个题倒计时数
        countNum: 0, //倒计时初始值
        countTotalNum: 0, //总计时初始值
        nextDelayTime: 1, //答题后延时
        is_correct: -1, //当前题目是否正确
        correct_num: 0, //当前题目选择的答案
        answer_true_num: 0, // 答对数目
        answer_false_num: 0, //答错数目
        begin_date: '', //开始答题时间
        tiperfen: 10, //每题分值
        node_id: '', //节点id
        reduce_step: 1000, //扣减部署
        is_last_node: 0, //是否最后节点
        duoxuan_select: '', //多选选择项
        oper_type:0,

    },
    // 以下是旧式的定义方式，可以保持对 <2.2.3 版本基础库的兼容
    attached: function () {
        //   console.log(9999999999)
    },
    /**
     * 组件的方法列表
     */
    methods: {
        start(a, b) {
            console.log(a, b)
            this.setData({
                countNumNomal: app.config.baseInfo.countdown,
                begin_date: util.formatTime(new Date()),
                tiperfen: app.config.baseInfo.tiperfen,
                oper_type: b,
                node_id: a.node_id || '', //节点id
                reduce_step: a.reduce_step || 1000, //扣减部署
                is_last_node: a.is_last_node || 0, //是否最后节点
                countTotalNum: 0,
                requestSuccess: !1,
                postSuccess: !1,
            });
            this.onShow();
        },
        checkboxChange: function (e) {
            var a = this
            for (var i = 0; i < this.data.answer.length; ++i) { //如果已经选过答案，不能再次选中
                if (this.data.list[this.data.index].id == this.data.answer[i].id) {
                    return
                }
            }
            this.setData({
                select_index: 99,
                duoxuan_select: a.arrtostr(e.detail.value.sort())
            })
            // console.log(e)
            // console.log('checkbox发生change事件，携带value值为：', e.detail.value)
            // console.log('checkbox发生change事件，携带value值为：.sort()', e.detail.value.sort())
            // console.log('checkbox发生change事件，携带value值为：.sort()', a.arrtostr(e.detail.value.sort()))

        },
        arrtostr(arr) {
            var restr = ''
            for (var i in arr) {
                restr = restr + arr[i];
            }
            return restr;
        },
        reset() {

        },
        onCheck: function (t) {
            for (var i = 0; i < this.data.answer.length; ++i) { //如果已经选过答案，不能再次选中
                if (this.data.list[this.data.index].id == this.data.answer[i].id) {
                    return
                }
            }
            // console.log(t.currentTarget.dataset.index)
            // console.log(t.currentTarget.dataset)
            // console.log(t.currentTarget)
            var a = 1 * t.currentTarget.dataset.index;
            this.setData({
                select_index: a
            }, () => {
                //this.onSubmit()
            });
        },
        onSubmit: function (a) {
            var a = this
            console.log(a.data.answer)
            a.stopCountADown(); //如果选中答案，则停掉倒计时
            var sleeps = a.data.nextDelayTime
            let false_num = a.data.answer_false_num
            let true_num = a.data.answer_true_num
            if (this.data.select_index < 0) {
                false_num++
                a.setData({
                    is_correct: 0,
                    correct_num: a.data.list[a.data.index].answer,
                    answer_false_num: false_num
                })
                a.data.answer.push({
                    id: a.data.list[a.data.index].id,
                    answer: "",
                    is_correct: a.data.is_correct
                })
                sleeps = sleeps * 3
            } else {
                if (this.data.select_index == 99) {
                    if (a.data.duoxuan_select == a.data.list[a.data.index].answer) {
                        // sleeps=1
                        true_num++
                        a.setData({
                            is_correct: 1,
                            correct_num: a.data.list[a.data.index].answer,
                            answer_true_num: true_num
                        })
                    } else {
                        sleeps = sleeps * 3
                        false_num++
                        a.setData({
                            is_correct: 0,
                            correct_num: a.data.list[a.data.index].answer,
                            answer_false_num: false_num
                        })
                    }
                    a.data.answer.push({
                        id: a.data.list[a.data.index].id,
                        answer: a.data.duoxuan_select,
                        is_correct: a.data.is_correct
                    });
                } else {
                    if (a.data.list[a.data.index].detaillist[a.data.select_index].item == a.data.list[a.data.index].answer) {
                        // sleeps=1
                        true_num++
                        a.setData({
                            is_correct: 1,
                            correct_num: a.data.list[a.data.index].answer,
                            answer_true_num: true_num
                        })
                    } else {
                        sleeps = sleeps * 3
                        false_num++
                        a.setData({
                            is_correct: 0,
                            correct_num: a.data.list[a.data.index].answer,
                            answer_false_num: false_num
                        })
                    }
                    a.data.answer.push({
                        id: a.data.list[a.data.index].id,
                        answer: a.data.list[a.data.index].detaillist[a.data.select_index].item,
                        is_correct: a.data.is_correct
                    });
                }
            }
            // console.log(this.data.answer)
            // sleeps=0
            this.data.index < this.data.total - 1 ? this.goNext(sleeps) : this.data.oper_type == 1 ? this.loadDataTest(sleeps) : this.submit(sleeps);
        },
        goNext(s) {
            // console.log(this.data.index)
            // console.log(this.data.select_index)
            // console.log(this.data.list[this.data.index].answer)
            // console.log(this.data.list[this.data.index].id)
            // console.log(this.data.list[this.data.index].detaillist[this.data.select_index].item||'')
            var a = this
            let is_correct = a.data.is_correct
            wx.showToast({
                title: is_correct == 1 ? '回答正确' : '回答错误',
                //icon: is_correct==1?'success':'error',
                image: is_correct == 1 ? '/images/ok.png' : '/images/error.png',
                mask: true,
                duration: 500
            })
            setTimeout(() => {
                this.setData({
                    index: ++this.data.index,
                    select_index: -1,
                    is_correct: -1,
                    correct_num: 0
                }, () => {
                    wx.hideToast();
                    this.countADown(); //下一题后 重新计时
                })
            }, 500)
        },
        loadData: function () {
            var a = this;
            wx.showLoading({
                title: ""
            });
            wx.request({
                url: app.domain + "ssm/appWebServ/answer/tklist",
                data: {
                    tkcnt: 5
                },
                success: (res) => {
                    console.log(res.data)
                    if ("0" == res.data.respCode) {
                        a.setData({
                            total: res.data.list.length,
                            list: res.data.list,
                            //                            logid: res.data.logid,
                            requestSuccess: !0,
                            index: 0,
                            select_index: -1,
                            answer: [],
                            is_correct: -1,
                            correct_num: 0,
                            answer_true_num: 0, // 答对数目
                            answer_false_num: 0, //答错数目
                            end_date: '', //结束答题时间
                            correct_rate: '', //正确率
                        });
                    }

                },
                complete: () => {
                    wx.hideLoading();
                    this.countADown();
                }
            })

            // a.setTimeCount()
        },
        loadDataTest: function (s) {
            var a = this;
            let is_correct = a.data.is_correct
            wx.showToast({
                title: is_correct == 1 ? '回答正确' : '回答错误',
                //icon: is_correct==1?'success':'error',
                image: is_correct == 1 ? '/images/ok.png' : '/images/error.png',
                mask: true,
                duration: 500
            })
            setTimeout(() => {
                wx.showLoading({
                    title: ""
                });
                wx.request({
                    url: app.domain + "ssm/appWebServ/answer/tklist",
                    data: {
                        tkcnt: 5
                    },
                    success: (res) => {
                        console.log(res.data)
                        if ("0" == res.data.respCode) {
                            a.setData({
                                total: res.data.list.length,
                                list: res.data.list,
                                //                            logid: res.data.logid,
                                requestSuccess: !0,
                                index: 0,
                                select_index: -1,
                                answer: [],
                                is_correct: -1,
                                correct_num: 0,
                                answer_true_num: 0, // 答对数目
                                answer_false_num: 0, //答错数目
                                end_date: '', //结束答题时间
                                correct_rate: '', //正确率
                            });
                        }

                    },
                    complete: () => {
                        wx.hideLoading();
                        wx.hideToast();
                        this.countADown();
                    }
                })
            }, 500)

            // a.setTimeCount()
        },
        submit: function (s) {
            // 停掉倒计时
            var a = this
            let is_correct = a.data.is_correct
            wx.showToast({
                title: is_correct == 1 ? '回答正确' : '回答错误',
                //icon: is_correct==1?'success':'error',
                image: is_correct == 1 ? '/images/ok.png' : '/images/error.png',
                mask: true,
                duration: 500
            })
            setTimeout(() => {
                // console.log(this.data.logid)
                // console.log(this.data.answer)
                // console.log(app.openid)
                // console.log(this.data.answer.length)
                // console.log(this.data.answer_true_num)
                // console.log(this.data.answer_false_num)
                // console.log(this.data.begin_date)
                // console.log(this.data.countTotalNum)
                var end_date = util.formatTime(new Date())
                var correct_rate = (this.data.answer_true_num / this.data.answer.length).toFixed(2)
                // console.log(end_date)
                // console.log(correct_rate)
                this.stopcountTotalDown()
                wx.showLoading({
                    title: ""
                });
                let datalist = {
                    list: a.data.answer,
                    openid: app.openid,
                    node_id: a.data.node_id,
                    answer_num: a.data.answer.length,
                    answer_true_num: a.data.answer_true_num,
                    answer_false_num: a.data.answer_false_num,
                    begin_date: a.data.begin_date,
                    use_time: a.data.countTotalNum,
                    end_date: end_date,
                    correct_rate: correct_rate,
                    reduce_step: a.data.reduce_step,
                    is_last_node: a.data.is_last_node,
                }

                wx.request({
                    url: app.domain + "ssm/appWebServ/answer/answerSave",
                    data: {
                        jsonStr: datalist
                    },
                    success: (res) => {
                        //this.triggerEvent('onfinish', {name: "hello"}) 
                        //  console.log('save；',res.data)
                        //   if("0"==res.data.respCode){
                        //       a.setData({
                        //           total: res.data.list.length,
                        //           list: res.data.list,
                        //           logid: res.data.logid,
                        //           requestSuccess: !0,
                        //           index: 0,
                        //           select_index: -1,
                        //           answer:[],
                        //           is_correct:-1,
                        //           correct_num:0,
                        //           answer_true_num:0,// 答对数目
                        //           answer_false_num:0,//答错数目
                        //           end_date:'',//结束答题时间
                        //           correct_rate:'',//正确率
                        //       });
                        //   }

                    },
                    complete: () => {
                        wx.hideLoading();
                        wx.hideToast();
                        this.setData({
                            info: {
                                oknum: this.data.answer_true_num,
                                desc: correct_rate * 100 + '%',
                                score: this.data.answer_true_num * this.data.tiperfen,
                                errnum: this.data.answer_false_num
                            },
                            countTotalNum: 0,
                            requestSuccess: !1,
                            postSuccess: !0,
                        })
                        this.triggerEvent('onfinish', {
                            name: "finish"
                        })
                    }
                })

                //   let data = {
                //       oknum:this.data.answer_true_num,
                //       desc:correct_rate*100+'%',
                //       score:this.data.answer_true_num*this.data.tiperfen,
                //       errnum:this.data.answer_false_num
                //   }
                //   data = JSON.stringify(data);
                //   wx.navigateTo({
                //       url: "/pages/answer/result?data="+data
                //   })
                //   this.setData({
                //       countTotalNum:0,
                //       postSuccess:!0
                //   })

            }, 500)

            // t.net.post(t, t.url.URL_ANSWER_COMPLATE, {
            //     logid: this.data.logid,
            //     answer: this.data.answer
            // }, function(a) {
            //     a.error ? t.msg(a.msg) : (t.setData("complete", a.data), wx.redirectTo({
            //         url: "/pages/answer/result"
            //     }));
            // });
        },
        onShow: function () {
            this.loadData();
            //什么时候触发倒计时，就在什么地方调用这个函数
            this.countADown(); //每个题倒计时
            this.countTotalDown(); //总计时
        },
        countADown: function () {
            this.resetCountADown();
            let that = this;
            let countNum = that.data.countNumNomal; //获取倒计时初始值
            //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
            that.setData({
                timer: setInterval(function () {
                    countNum--;
                    that.setData({
                        countNum: countNum
                    })
                    if (countNum == 0) {
                        that.onSubmit()
                        that.resetCountADown();
                        //that.onSubmit()
                    }
                }, 1000)
            })
            //   that.data.timer = setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
            //     //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
            //     if (countNum == 0) {
            //       wx.showToast({
            //         title: '请选择答案..',
            //       })
            //       clearInterval(that.data.timer);
            //       //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
            //       //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
            //        //clearInterval(that.data.timer);
            //       //关闭定时器之后，可作其他处理codes go here
            //     }else{
            //     //每隔一秒countNum就减一，实现同步
            //     countNum--;
            //     //然后把countNum存进data，好让用户知道时间在倒计着
            //     that.setData({
            //         countNum: countNum
            //     })}
            //   }, 1000)
        },
        stopCountADown: function () {
            clearInterval(this.data.timer);
        },
        resetCountADown: function () {
            this.stopCountADown();
            this.setData({
                countNum: this.data.countNumNomal
            })
        },
        countTotalDown: function () { //总计时 
            let that = this;
            let countTotalNum = that.data.countTotalNum; //获取倒计时初始值 
            that.data.totaltimer = setInterval(function () {
                countTotalNum++;
                that.setData({
                    countTotalNum: countTotalNum
                })
            }, 1000)
        },
        stopcountTotalDown: function () {
            clearInterval(this.data.totaltimer);
        },
    },
})